//
//  QTHomeTableVC.m
//  QTLifeCircle
//
//  Created by 王琼 on 17/1/15.
//  Copyright © 2017年 徐腾. All rights reserved.

#import "QTHomeTableVC.h"
#import "QTLifeCircleNetworkingTool.h"
#import "QTGuessModel.h"
#import "QTHeadItemModel.h"
#import "QTHomeGuessCell.h"
#import "QTHomefamousCell.h"
#import "QTHeardLoopViewModel.h"
#import "QTHeadView.h"
#import "QTFamousChildController.h"
#import <SDCycleScrollView.h>
#import <YYModel.h>
//------第一个cell数据处理------
#import"QTFmousModel.h"

@interface QTHomeTableVC ()<SDCycleScrollViewDelegate,QTHomefamousCellDelegate,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *bannerView;

@property (weak, nonatomic) IBOutlet UICollectionView *headCollectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionViewFlowLayout;

@property(nonatomic, weak) IBOutlet QTHeadView * headView;


@end

@implementation QTHomeTableVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadData];
//    [self loadBannerView];
    self.tableView.showsVerticalScrollIndicator = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)loadData
{
    
    NSDictionary* dict = @{@"anu":@"api/1/index/get_index_info"};
    [[QTLifeCircleNetworkingTool sharedManager] POSTWithURLStr:@"http://iosapi.itcast.cn/life/homeData.json.php" parameter:dict compeletionHandle:^(id responseObject) {
        
        //拿到猜你喜欢模块的数据
        NSArray* array = responseObject[@"guess"][@"list"];
        //创建临时数组存放数据  字典数组---模型数组
        //方法一
        self.guessModelList = [NSArray yy_modelArrayWithClass:[QTGuessModel class] json:array];
        
        [self.tableView reloadData];
        
        //方法二
        //------第一个cell数据处理------
        NSArray* arrayf = responseObject[@"famous"][@"list"];
        NSMutableArray* arrTemp = [NSMutableArray array];
        for (NSDictionary* dict in arrayf)
        {
            QTFmousModel* guessM = [QTFmousModel yy_modelWithDictionary:dict];
            [arrTemp addObject:guessM];
        }
        self.famousModelList = arrTemp;
        NSLog(@"----%@",self.famousModelList);
        
        [self.tableView reloadData];
        
        
        //拿到猜你喜欢模块的数据
        NSArray* arrayH = responseObject[@"focus"][@"list"];
        //创建临时数组存放数据  字典数组---模型数组
        //方法一
        NSMutableArray* arr = [NSMutableArray array];
        NSMutableArray* arrP = [NSMutableArray array];
        
        for (NSDictionary* dict in arrayH )
        {
            //测试笔记:断点达到这的时候 model是有值的
            QTHeardLoopViewModel* model = [QTHeardLoopViewModel yy_modelWithDictionary:dict];
            
            [arr addObject:model.title];
            [arrP addObject:model.cover];
            
        }
        //断点达到这里,arr 和 arrP也有值
        //拿到图片轮播器的地址
        self.looPTitleList = arr;
        self.loopPicUrls = arrP;
        NSLog(@"图片地址%@",self.loopPicUrls);
        
        [self loadBannerView];
        
        [self.tableView reloadData];
        
        
        //------表头cell数据处理------
        NSArray* arrayC = responseObject[@"group"][@"list"];
        NSMutableArray* arrC = [NSMutableArray array];
        for (NSDictionary* dict in arrayC)
        {
            QTHeadItemModel* model = [QTHeadItemModel yy_modelWithDictionary:dict];
            [arrC addObject:model];
        }
        //self.headModelList = arrC;
        
        //到这里self.headModelList有值,但是self.headView.modelList报错
        //error: use of undeclared identifier 'self'
        self.headView.modelList = arrC;
        
        [self.tableView reloadData];
        

    }];

}

/**
 加载轮播视图
 */
- (void)loadBannerView {
//    [self.tableView reloadData];
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame: self.bannerView.bounds delegate:self placeholderImage:nil];
    
    cycleScrollView.imageURLStringsGroup = self.loopPicUrls;
    cycleScrollView.titlesGroup = self.looPTitleList;
    //要用addSubview的方法 直接赋值无效
    [self.bannerView addSubview:cycleScrollView];
    
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    if (section == 0) {
        return 1;
    }else{
    return self.guessModelList.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QTHomeGuessCell *cell;
    
    if (indexPath.section == 0) {
        QTHomefamousCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor blackColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
           //------第一个cell数据处理------
            //拿到famousModeList的数据
            cell.famousModelList = self.famousModelList;
        
           //代理
            cell.delegate = self;
           //------第一个cell数据处理------
        return cell;
    }
    else if(indexPath.section == 1)
    {
      QTHomeGuessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        QTGuessModel *model = self.guessModelList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.guessModel = model;
          return cell;
    }
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 )
    {
        return 300;
    }else
    {
      return  100;
    }
    
}

//返回组头组尾的方法
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if(section == 0)
   {
     return @"名店推荐";
   }else if(section == 1)
    {
     return @"猜你喜欢";
    }
    return @"焦头烂额";
}

/*
 QTHomefamousCellDelegate
 */
-(void)pushToController:(QTFmousModel *)model
{
    QTFamousChildController* conVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil]instantiateViewControllerWithIdentifier:@"ChildControll"];
    //在这里将数据传给控制器
    //分析:这里将indexPath.item的model
    conVC.famousModel = model;
    [self.navigationController pushViewController:conVC animated:YES];
}



@end
